// import request from '../../utils/request'

// pages/videoPlayer/videoPlayer.js
import { dateNew } from '../../common/utils';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 视频信息
    videoinfo: {},
    // 视频url
    videoUrl: '',
    relatedallvideo: {},
    videodetailinfo: {},
    color: {
      dian_zan: '',
      shoucang: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.getVideoUrl(options.id)
    this.getilVideoInfo(options.id)
    this.getXiangGuanVideo(options.id)
    // this.getDetailVideoInfo(options.id)
  },

  // 根据获得的videoId查询video的url
  async getVideoUrl (id) {
    let videoUrl = await wx.$http.get('/video/url', { id })
    this.setData({
      videoUrl: videoUrl.urls[0].url
    })
  },
  // 获取相关视频
  async getXiangGuanVideo (id) {
    let { data } = await wx.$http.get('/related/allvideo', { id })
    this.setData({
      relatedallvideo: data
    })
  },
  // 视频点赞转发评论数数据
  // async getDetailVideoInfo (vid) {
  //   let { data } = await wx.$http.get('/video/detail/info', { vid })
  //   this.setData({
  //     videodetailinfo: data
  //   })
  // },
  //视频详情
  async getilVideoInfo (id) {
    let resdata = await wx.$http.get('/video/detail', { id })
    // 转换视频发布时间  为  YY:HH:MM 格式
    let _newdate = dateNew(resdata.data.publishTime)
    resdata.data.publishTime = _newdate.Y + ' 年 ' + _newdate.m + ' 月 ' + _newdate.d + ' 日 '
    this.setData({
      videoinfo: resdata.data
    })
  },
  // 页面下拉刷新
  onPullDownRefresh () {
    // 获取视频vid
    let { vid } = this.data.videoinfo
    // 重新请求数据
    this.getVideoUrl(vid)
    this.getilVideoInfo(vid)
    this.getXiangGuanVideo(vid)
    // 延时关闭下拉刷新动画
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 800)
  },
  // 视频点赞  =====由于接口问题  更新不了数据  就暂时做个假的
  async tapDianZan (event) {
    // 获取数据
    let { color, videoinfo } = this.data
    // 判断是否点赞过
    if (color.dian_zan === '') {
      let id = videoinfo.vid
      // 发起请求
      let res = await wx.$http.get('/resource/like', { t: 1, type: 5, id })
      if (res.code == 200) {
        // let resifon = await wx.$http.get('/video/detail/info', { vid: id })
        // console.log(resifon);
        // this.getilVideoInfo(id)
        // 更改颜色
        color.dian_zan = 'rgb(255, 0, 0)'
        // 点赞数据 +1   
        videoinfo.praisedCount = videoinfo.praisedCount + 1
      }
    } else {
      // 更改颜色
      color.dian_zan = ''
      // 点赞数据 -1   
      videoinfo.praisedCount = videoinfo.praisedCount - 1
    }
    this.setData({
      color,
      videoinfo
    })
  },
  // 收藏视频  
  /**
   * 由于接口问题  更新不了数据  就暂时做个假的
   * 
   * **/
  async tapShouCang () {
    let { color, videoinfo } = this.data
    if (color.shoucang === '') {
      color.shoucang = 'rgb(255, 0, 0)'
      videoinfo.subscribeCount = videoinfo.subscribeCount + 1
    } else {
      color.shoucang = ''
      videoinfo.subscribeCount = videoinfo.subscribeCount - 1
    }
    this.setData({
      color,
      videoinfo
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function ({ from }) {
    console.log(from);
    if (from === 'button') {
      return {
        title: this.data.videoinfo.title,
        imageUrl: this.data.videoinfo.coverUrl
      }
    } else if (from === 'menu') {
      return {
        title: this.data.videoinfo.title,
        imageUrl: this.data.videoinfo.coverUrl
      }
    }
  },
  // 点击看相关视频
  itemVideotop (e) {
    console.log(e);
    // 在使用data-key 传值的时候不要写大写字母  它会自动转化为小写
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let { itemvid } = e.currentTarget.dataset
    this.getVideoUrl(itemvid)
    this.getilVideoInfo(itemvid)
    this.getXiangGuanVideo(itemvid)
    setTimeout(() => {
      wx.hideLoading()
    }, 1000)
  }
})